import { songsType } from '@renderer/store/type'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import _ from 'loadsh'


export const usePlayListStore = defineStore('playListStore', () => {
  // 播放列表
  const playList = ref<songsType[]>([])
  // 当前播放歌曲id
  const playId = ref(0)
  // 下一首播放歌曲id
  const nextPlayId = ref(0)
  // 上一首播放歌曲id
  const prevPlayId = ref(0)
  //播放模式 0 顺序播放 1 单曲循环 2 随机播放
  const playMode = ref(0)
  // 暂停播放
  const pauseOrPlay = () => {
    // 暂停播放
    const audio = document.querySelector('audio')
    let currentTime = 0
    if (!audio) {
      return
    }
    if (audio.paused) {
      // 播放
      audio.currentTime = currentTime
      audio.play()
    } else {
      //  保留播放进度
      currentTime = audio.currentTime
      // 暂停播放
      audio.pause()
    }
  }
  // 将歌单歌曲全部添加到播放列表
  const addAllSongToPlayList = (songs: songsType[]) => {
    playList.value = []
    playList.value = songs
    // 默认播放第一首歌
    playId.value = playList.value[0].id
  }
  // 添加单曲到播放列表
  const addSongToPlayList = (song: any) => {
    // 类型断言
    const songData = song as songsType
    // 检查歌曲是否已经在播放列表中
    if (playList.value.some((item) => item.id === song.id)) {
      return
    }
    playList.value.push(songData)
  }
  // 获取下一首歌曲id
  const getNextSongId = () => {
    if (playMode.value === 2) {
      // 随机播放
      const randomIndex = Math.floor(Math.random() * playList.value.length)
      playId.value = playList.value[randomIndex].id
      return
    }
    //  findIndex方法返回数组中满足提供的测试函数的第一个元素的索引。如果没有找到符合条件的元素，则返回 -1。
    let index = playList.value.findIndex((item) => item.id === playId.value)

    if (index !== -1) {
      //当前播放歌曲不为最后一首
      if (index === playList.value.length - 1) {
        // 当前播放为最后一首时，点击下一首，则播放第一首
        nextPlayId.value = playList.value[0].id
      } else {
        nextPlayId.value = playList.value[index += 1]?.id
      }
    } else {
      nextPlayId.value = playList.value[0].id
    }

    playId.value = nextPlayId.value
  }
  // 获取上一首歌曲id
  const getPrevSongId = () => {
    if (playMode.value === 2) {
      // 随机播放
      const randomIndex = Math.floor(Math.random() * playList.value.length)
      playId.value = playList.value[randomIndex].id
      return
    }
    if (playMode.value === 1) {
      // 单曲循环
      playId.value = prevPlayId.value
      return
    }
    let index = playList.value.findIndex((item) => item.id === playId.value)

    if (index !== 0) {
      //当前播放歌曲不为第一首
      prevPlayId.value = playList.value[index -= 1]?.id
    } else {
      index = playList.value.length - 1
      // 当前播放为第一首时，点击上一首，则播放最后一首
      prevPlayId.value = playList.value[index].id
    }
    playId.value = prevPlayId.value
  }
  const changePlayMode = () => {
    if (playMode.value === 0) {
      playMode.value = 1
      // 随机播放
      // playList.value = _.shuffle(playList.value)
    } else if (playMode.value === 1) {
      playMode.value = 2
    } else {
      playMode.value = 0
    }
  }

  return {
    playList,
    nextPlayId,
    prevPlayId,
    playId,
    playMode,
    addAllSongToPlayList,
    getNextSongId,
    getPrevSongId,
    addSongToPlayList,
    pauseOrPlay,
    changePlayMode
  }
})
